/**
 * Copyright 2019-2020 Huawei Technologies Co., Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef FUSION_ENGINE_UTILS_TASK_BUILDER_TASK_BUILDER_H_
#define FUSION_ENGINE_UTILS_TASK_BUILDER_TASK_BUILDER_H_
#include <map>
#include <memory>
#include <vector>
#include "proto/task.pb.h"
#include "common/taskdown_common.h"
#include "adapter/adapter_itf/task_builder_adapter.h"
#include "common/opskernel/ops_kernel_info_types.h"

namespace fe {
Status CreateHandle(ccHandle_t *handle);
Status DestroyHandle(ccHandle_t *handle);

class TaskBuilder {
 public:
  TaskBuilder();
  virtual ~TaskBuilder();

  /*
   * @ingroup fe
   * @brief   Generate tasks
   * @param   [in] node Node of compute graph
   * @param   [in] context Context for generate tasks
   * @param   [out] task_defs Save the generated tasks.
   * @return  SUCCESS or FAILED
   */
  Status GenerateTask(const ge::Node &node, const ge::RunContext &context, std::vector<domi::TaskDef> &task_defs);

 private:
  TaskBuilder(const TaskBuilder &builder) = delete;
  TaskBuilder &operator=(const TaskBuilder &builder) = delete;

  Status DoGenerate(const ge::Node &node);

  /*
   * @ingroup fe
   * @brief   Create TaskBuilderAdapter
   * @param   [in] node Node of compute graph
   * @return  SUCCESS or FAILED
   */
  Status CreateAdapter(const ge::Node &node);

  /*
   * @ingroup fe
   * @brief   Run TaskBuilderAdapter
   * @return  SUCCESS or FAILED
   */
  Status RunAdapter();

  /*
   * @ingroup fe
   * @brief   The callback for runtime
   * @return  ACL_RT_SUCCESS or other
   */
  static rtError_t TaskGenCallback(rtModel_t model, rtTaskInfo_t *task_info);

  static rtError_t GenKernelTask(const TaskBuilder &builder, const rtTaskInfo_t &task_info, domi::TaskDef &task_def);

  static rtError_t GenAllKernelTask(const TaskBuilder &builder, const rtTaskInfo_t &task_info,
                                    domi::TaskDef &task_def);

  static Status FillKernelContext(const vector<uint32_t> &orig_op_indexes, const rtKernelTaskInfo_t &kernel_task,
                                  const ccOpContext &op_context, domi::KernelContext &kernel_context);

  static Status FillFlowtable(const domi::KernelContext &kernel_context, const rtKernelTaskInfo_t &kernel_task,
                              domi::KernelDef &kernel_def);

  static Status FillKernelContextMultiKernel(const vector<uint32_t> &orig_op_indexes,
                                             const rtAllKernelTaskInfo_t &kernel_task,
                                             const ccOpContext &op_context, domi::KernelContext &kernel_context);

  static TaskBuilder *GetTaskBuilderByModel(const rtModel_t &model);
  void AddTaskBuilder();
  void RemoveTaskBuilder();

 private:
  TaskBuilderContext context_;
  TaskBuilderAdapterPtr adapter_{nullptr};
  std::vector<domi::TaskDef> *task_defs_{nullptr};

  std::vector<uint32_t> orig_op_indexes_;

  static std::map<rtModel_t, TaskBuilder *> task_builder_map_;
};

}  // namespace fe
#endif  // FUSION_ENGINE_UTILS_TASK_BUILDER_TASK_BUILDER_H_
